#include "drv_led1.h"
#include "drv_systick.h"
#include "drv_key.h"
#include "drv_usart.h"
#include "drv_time.h"
#include "bmp.h"
#include "oled.h"
#include "drv_w25qxx.h"
#include "drv_dma.h"
#include "stdio.h"
#include "drv_adc.h"
#include "string.h"
#include "drv_dht11.h"
#include "drv_exti.h"
//led_val为灯状态、beep_val为蜂鸣器状态、
uint8_t led_val = 0,beep_val=0,key_val3=0, key_val4=0 ; 
uint8_t gmr_flag = 0 ;//
uint16_t led_pwm = 0 ; //LED强度值
uint32_t buf1[5] = {0x11110001,0x11110002,0x11110003,0x11110004,0x11110005};
uint32_t buf2[5] = {0};
uint8_t temp,humi;	//温湿度值
uint8_t page_flag = 1; //当前页面
uint16_t current = 0 ; //光敏电阻采样值
char show_msg[100];
u8 msg[100] ;

//int main(void)
//{
//	NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2);//设置中断组别
//	drv_usart2_init();//串口初始化
//	drv_usart_init();//串口初始化
//	drv_systick_init(168);//计时器初始化
//	//printf("aaa");
//	
//	usart_sendstr(USART2,"AT+RESTORE\r\n", strlen("AT+RESTORE\r\n"));
//	drv_systick_delayms(2000);

//	usart_sendstr(USART2,"AT\r\n", strlen("AT\r\n"));
//	drv_systick_delayms(500);
//	usart_sendstr(USART2,"AT+CWMODE=3\r\n", strlen("AT+CWMODE=3\r\n"));
//	drv_systick_delayms(500);
//	usart_sendstr(USART2,"AT+CIPMUX=0\r\n", strlen("AT+CIPMUX=0\r\n"));
//	drv_systick_delayms(500);
//	usart_sendstr(USART2,"AT+CWJAP=\"hqyjcs\",\"hqyj2022\"\r\n", strlen("AT+CWJAP=\"hqyjcs\",\"hqyj2022\"\r\n"));
//	drv_systick_delayms(20000);
//	usart_sendstr(USART2,"AT+CIPSTART=\"TCP\",\"192.168.0.153\",9011\r\n", strlen("AT+CIPSTART=\"TCP\",\"192.168.0.153\",9011\r\n"));
//	drv_systick_delayms(5000);
//	usart_sendstr(USART2,"AT+CIPSEND\r\n", strlen("AT+CIPSEND\r\n"));
//	drv_systick_delayms(500);
//	while(1);
//	
//}
//////////////////////////////////////////////////////////
//第四页面信息转换函数
void change(char* str,int size)
{
	int i ;
	for (i = 0; i < size; ++i)
	{
		msg[i] = (u8)str[i];
	}
}

int main(void)
{
	NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2);//设置中断组别
	drv_led1_init() ;//lED初始化
	drv_time1_init();//高级定时器1
	drv_spk_init();//蜂鸣器初始化
	drv_systick_init(168);//计时器初始化
	//drv_key_init();//按键初始化
	drv_usart_init();//串口1初始化
	drv_usart2_init();//串口2初始化
	//drv_time6_init();//基本定时器6初始化
	drv_exti_init();//按键外部中断
	drv_spi_init();
	DHT11_Init();//温湿度初始化

//	printf("id:%0x",drv_w25qxx_readID());
//获取光敏采样值初始化
	drv_dma2_init(5, buf2, buf1) ;
	drv_adc_init();
//	while(DMA_Getkey_flagStatus(DMA2_Stream0,DMA_key_flag_TCIF0) == RESET);
//	drv_dmaData_compare(buf1,buf2);
//获取光敏采样值

	usart_sendstr(USART2,"AT+RESTORE\r\n", strlen("AT+RESTORE\r\n"));
	drv_systick_delayms(2000);
	usart_sendstr(USART2,"AT\r\n", strlen("AT\r\n"));
	drv_systick_delayms(500);
	usart_sendstr(USART2,"AT+CWMODE=3\r\n", strlen("AT+CWMODE=3\r\n"));
	drv_systick_delayms(500);
	usart_sendstr(USART2,"AT+CIPMUX=0\r\n", strlen("AT+CIPMUX=0\r\n"));
	drv_systick_delayms(500);
	//usart_sendstr(USART2,"AT+CWJAP=\"hqyjcs\",\"hqyj2022\"\r\n", strlen("AT+CWJAP=\"hqyjcs\",\"hqyj2022\"\r\n"));
	usart_sendstr(USART2,"AT+CWJAP=\"sweetlemon\",\"123456789\"\r\n", strlen("AT+CWJAP=\"sweetlemon\",\"123456789\"\r\n"));
	drv_systick_delayms(7000);
	usart_sendstr(USART2,"AT+CIPSTART=\"TCP\",\"192.168.183.60\",9011\r\n", strlen("AT+CIPSTART=\"TCP\",\"192.168.183.60\",9011\r\n"));
	drv_systick_delayms(5000);
	
	OLED_Init();//OLED初始化
	OLED_Clear();//清屏
	Show_Page1();//显示页面1
	while(1)
	{
////////////////第二页
		if(page_flag == 2)
		{
			current = drv_adc_res_value();//获取光敏采样值
			OLED_ShowNum(90,35,current,4,16,1);
			OLED_Refresh();//刷新
			//占空比根据光敏电阻采样显示光强
			if (current > 3500)
			{
				led_pwm = 300 ;
				led2_show_open();
			}
			else if (current < 3500&& current > 2500)
			{
				led_pwm = 150 ;
				led2_show_open();
			}
			else if (current < 2500&& current > 1500)
			{
				led_pwm = 50 ;
				led2_show_open();
			}
			else
			{
				led_pwm = 0 ;
			    led2_show_close();
			}
			TIM_SetCompare1(TIM1, led_pwm) ;
			//drv_systick_delayms(500);
		}
		
////////////////第二页		
////////////////第三页
		
		if(page_flag == 3)
		{
			DHT11_Read_Data(&temp,&humi);//获取温湿度值
			OLED_ShowNum(30,20,temp,2,16,1);//显示温度
			OLED_ShowNum(30,40,humi,3,16,1);//显示湿度
			OLED_Refresh();//刷新  
			//drv_systick_delayms(500);//延时
		}
////////////////第三页
////////////////第四页
		if(page_flag == 4)
		{
			change(show_msg,sizeof(show_msg));
			OLED_ShowString(35,25,msg,16,1);
			//printf("1:%s",show_msg);
			printf("2:%s",msg);
			Show_Page4();
			OLED_Refresh();//刷新  
			//drv_systick_delayms(500);//延时
		}
////////////////第四页		
		drv_systick_delayms(300);//延时
	}
}	

/////////////////////////////////////////////////////
	

///////////////////////////////////////////////////////////
	//printf("hello,23");
	//drv_led1_open() ;
//占空比
//	while (1)
//	{
//		if (key_flag == 0 )
//		{
//			led_pwm++ ;
//		}
//		else
//		{
//			led_pwm-- ;
//		}
//		if (led_pwm > 300)
//		{
//			key_flag = 1 ;
//		}
//		if (led_pwm == 0)
//		{
//			key_flag = 0 ;
//		}
//		TIM_SetCompare1( TIM1, led_pwm ) ;
//		drv_systick_delayms(10) ;
//	}
		//drv_systick_delayms(100);



